package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.Film;
import pojo.Tuser;
import service.FilmService;
import service.tuserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class getcontroller {
        @Resource(name="os")
        tuserService to;
        @Resource(name="oo")
    FilmService jb;

    public FilmService getJb() {
        return jb;
    }

    public void setJb(FilmService jb) {
        this.jb = jb;
    }

    public tuserService getTo() {
        return to;
    }

    public void setTo(tuserService to) {
        this.to = to;
    }

    @RequestMapping("login")
    public  String login(HttpServletRequest req, String user, String pass){
        Tuser tuser=to.login(user,pass);
        req.getSession().setAttribute("lo",tuser);
        if (tuser!=null){
            return "pegeAll";
        }else {
            return "login.jsp";
        }
    }

    @RequestMapping("pegeAll")
    public String getpage(HttpServletRequest req){
        Integer pageindex;
        if(req.getParameter("pageIndex")!=null)
            pageindex=Integer.parseInt(req.getParameter("pageIndex"));
        else
            pageindex=1;

        int pageSize=4;
        int count=jb.count();
        int totalpages=(count%pageSize==0)?(count/pageSize):(count/pageSize+1);

        if(pageindex<1){
            pageindex=1;
        }else if(pageindex>totalpages){
            pageindex=totalpages;
        }

        int begin=(pageindex-1)*pageSize;

        List<Film> list= jb.getBypage(begin,pageSize);

        req.getSession().setAttribute("all", list);
        req.getSession().setAttribute("co", totalpages);
        req.getSession().setAttribute("to", pageindex);
        return "index.jsp";
    }

    @RequestMapping("delOrder")
    public String delete(HttpServletRequest req){
        int num=jb.delete(Integer.parseInt(req.getParameter("id")));
        if(num>0){
            return "pegeAll";
        }
        else{
            return "pegeAll";
        }
    }

    @RequestMapping("insert")
    public String insert(HttpServletRequest req,Film fo){
        jb.insert(fo);
        return "pegeAll";
    }
    @RequestMapping("update")
    public  String update(HttpServletRequest req,Film ff){
        jb.update(ff);
        return "pegeAll";
    }
    @RequestMapping("selectid")
    public  String selectid(HttpServletRequest req, int id){
        Film S=jb.selectid(id);
        req.getSession().setAttribute("cid",S);

        return  "update.jsp";
    }

}
